/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gac.gacmod.visual;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.LinkedList;
import java.util.List;
import org.netbeans.api.visual.router.Router;
import org.netbeans.api.visual.widget.ConnectionWidget;
import org.netbeans.api.visual.widget.Widget;

/**
 *
 * @author Tomek
 */
public class SimpleOrthogonalRouter implements Router
{

    public static SimpleOrthogonalRouter upperLinksRouter = new SimpleOrthogonalRouter(true);
    public static SimpleOrthogonalRouter lowerLinksRouter = new SimpleOrthogonalRouter(false);
    private boolean _upperConnection;

    private SimpleOrthogonalRouter(boolean upperConnection)
    {
        _upperConnection = upperConnection;
    }

    public List<Point> routeConnection(ConnectionWidget connectionWidget)
    {
        Widget srcWidget = connectionWidget.getSourceAnchor().getRelatedWidget();
        Rectangle srcRect = srcWidget.convertLocalToScene(srcWidget.getBounds());
        Point start = new Point(srcRect.x + (srcRect.width / 2),
                srcRect.y + srcRect.height);

        Widget dstWidget = connectionWidget.getTargetAnchor().getRelatedWidget();
        Rectangle dstRect = dstWidget.convertLocalToScene(dstWidget.getBounds());

        Point end = new Point(dstRect.x + (dstRect.width / 2),
                dstRect.y);

//        Point start = connectionWidget.getSourceAnchor().compute(connectionWidget.
//                getSourceAnchorEntry()).getAnchorSceneLocation();
//
//        Point end = connectionWidget.getTargetAnchor().compute(connectionWidget.
//                getTargetAnchorEntry()).getAnchorSceneLocation();

        int verticalDist = end.y - start.y - 15;

        Point mid1 = new Point(start.x, start.y + 15);

        Point mid2 = new Point(end.x, end.y - verticalDist);

        if (_upperConnection)
        {
            mid1 = new Point(start.x, start.y + 15);
            mid2 = new Point(end.x, end.y - verticalDist);
        } else
        {
            mid1 = new Point(start.x, start.y + verticalDist);
            mid2 = new Point(end.x, end.y - 15);
        }

        List<Point> result = new LinkedList<Point>();
        result.add(start);
        result.add(mid1);
        result.add(mid2);
        result.add(end);

        return result;
    }
}
